<template>
  <div class="app-container">
    <div style="display: flex; margin-bottom: 20px">
      <div>
        <el-input
          v-model="params.title"
          size="small"
          :clearable="true"
          placeholder="请输入新闻咨询标题"
        />
      </div>
      <div style="margin-left: 20px">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          @click="searchList"
        >搜索</el-button>
        <el-button type="primary" icon="el-icon-plus" size="small" style="margin-right:10px" @click="showAdd()">添加</el-button>
      </div>

    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="头像" width="110">
        <template slot-scope="scope">
          <img :src="scope.row.img" width="50px" height="50px">
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.tel }}
        </template>
      </el-table-column>
      <el-table-column prop="password" label="用户密码" width="160" align="center" show-overflow-tooltip />
      <el-table-column label="性别" align="center" width="110px">
        <template slot-scope="scope">
          <span v-if="scope.row.sex ==1">男</span>
          <span v-if="scope.row.sex ==2">女</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="状态" width="110" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status ==1" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status ==0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.created_at }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="评审员" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="新闻图片" width="110">
          <el-upload
            ref="uploadAvatar"
            class="upload-demo"
            action=""
            list-type="picture"
            :on-change="onChange1"
            :on-remove="onRemove1"
            :file-list="fileList1"
            :limit="1"
            :http-request="submitImage1"
          >
            <el-button
              slot="trigger"
              size="small"
              type="primary"
            >选取文件</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="手机号" prop="tel">
          <el-input v-model="form.tel" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio v-model="form.sex" label="1">男</el-radio>
            <el-radio v-model="form.sex" label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-pagination
      align="center"
      background
      :current-page="params.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="params.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 20px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import { getList } from '@/api/users'
import { Upload } from '@/api/users'
import { add } from '@/api/users'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      list: null,
      listLoading: true,
      form: {
        name: '',
        tel: '',
        sex: '1',
        img: '',
        password: ''
      },
      params: {
        title: '',
        page: 1,
        limit: 10
      },
      total: 1,
      fileList: [],
      fileList1: [],
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        sex: [{ required: true, message: '请选择性别', trigger: 'change' }],
        tel: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    showAdd() {
      this.dialogFormVisible = true
    },
    // 添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          add(this.form).then((res) => {
            if (res.code == '20000') {
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.dialogFormVisible = false
              this.fetchData()
            } else {
              this.$message({
                message: res.msg,
                type: 'error'
              })
            }
          })
        }
      })
    },
    onChange1(file, fileList1) {
      this.fileList1 = fileList1
    },
    onRemove1(file) {
      this.fileList1.map((item) => {
        if (item.uid == file.uid) {
          item.status = 'removed'
        }
      })
    },
    submitImage1() {
      const formData = new FormData()
      formData.append('path', this.fileList1[0].raw)
      Upload(formData).then((res) => {
        this.success1(res)
      })
    },
    success1(res) {
      if (res.code == '20000') {
        this.form.img = res.data.savePath
        this.$message.success('上传成功')
      } else {
        this.$message.error(res.message)
        // 请求失败后清空文件列表
        this.$refs.uploadAvatar.clearFiles()
      }
    },
    handleSizeChange(val) {
      this.params.limit = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.fetchData()
    },
    searchList() {
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.params).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    }
  }
}
</script>
